using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class FactoryObject
    {
        public static Factory[] GetFactories()
        {
            return Query(null);
        }

        public static Factory GetFactory(int id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(FactoryDesc.Id, ConditionExpress.EQUALS, id);

            Factory[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Factory GetFactory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(FactoryDesc.Name, ConditionExpress.EQUALS, name);

            Factory[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Factory[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + FactoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }

            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            if (table != null && table.Rows.Count > 0)
            {
                Factory[] factories = new Factory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Factory factory = new Factory();

                    factory.Id                   = Convert.ToInt32(table.Rows[i][FactoryDesc.Id]);
                    factory.Name                 = Convert.ToString(table.Rows[i][FactoryDesc.Name]);
                    factory.UserDefinedId        = Convert.ToString(table.Rows[i][FactoryDesc.UserDefinedId]);

                    factories.SetValue(factory, i);
                }

                return factories;
            }

            return null;
        }

        public static Int32 Add(Factory factory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(FactoryDesc.Name,             factory.Name);
            builder.InsertAdd(FactoryDesc.UserDefinedId,    factory.UserDefinedId);
            builder.QueryTableName = FactoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(FactoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = FactoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Factory factory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(FactoryDesc.Name,             factory.Name);
            builder.UpdateAdd(FactoryDesc.UserDefinedId,    factory.UserDefinedId);
            builder.ConditionAdd(FactoryDesc.Id, ConditionExpress.EQUALS, factory.Id);
            builder.QueryTableName = FactoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
